package cn.thens.kdroid.sample.storage

import android.app.Fragment
import android.content.Context
import cn.thens.kdroid.core.io.Crypto
import cn.thens.kdroid.core.io.Json
import cn.thens.kdroid.core.io.StringCodec
import cn.thens.kdroid.core.storage.Storage

class StorageFragment : Fragment() {
    override fun onResume() {
        super.onResume()
        if (context == null) return

        val prefs = context.getSharedPreferences("main", Context.MODE_PRIVATE)
        Crypto.aes("secrete".toByteArray()).toCodec()
        val node = Storage.node(prefs, "some_key")
                .codec(StringCodec.encryptToHex(Crypto.aes("secrete".toByteArray())).inverted())
                .codec(Json.toCodec<Person>())
                .cached().rx()

        node.get().doOnSuccess { }.subscribe()
        node.put(Person("Jack", "White")).subscribe()
        node.clear().subscribe()
        node.exists().subscribe()
    }

    data class Person(val firstName: String, val lastName: String)
}